package com.yango.vod.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.yango.serviceutil.exception.YangoException;
import com.yango.vod.service.VodService;
import com.yango.vod.utils.AliyunVodSDKUtil;
import com.yango.vod.utils.ReadPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author Li.Ding.yin
 * @create 2021年07月29日11:37-星期四
 * @role 视频点播业务层实现类
 */
@Service
public class VodServiceImpl implements VodService {

    //上传视频到阿里云-返回视频id
    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();
            //获取文件名
            String filename = file.getOriginalFilename();
            //对文件名处理-获取文件名称
            String title = filename.substring(0, filename.lastIndexOf("."));
            //上传
            UploadStreamRequest request = new UploadStreamRequest(ReadPropertiesUtil.ACCESS_KEY_ID,
                    ReadPropertiesUtil.ACCESS_KEY_SECRET, title, filename, inputStream);
            UploadVideoImpl upload = new UploadVideoImpl();
            UploadStreamResponse response = upload.uploadStream(request);
            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();//获取上传后的视频id
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
//                log.warn(errorMessage);
                if (StringUtils.isEmpty(videoId)) {
                    throw new YangoException(20001, errorMessage);
                }
            }
            return videoId;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YangoException(20001, "视频上传出错了!");
        }
    }

    //删除云端视频
    @Override
    public boolean removeVideo(String videoId) {
        try {
            //创建客户端client对象
            DefaultAcsClient client = AliyunVodSDKUtil.initVodClient(ReadPropertiesUtil.ACCESS_KEY_ID,
                    ReadPropertiesUtil.ACCESS_KEY_SECRET);
            //创建请求request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request对象中设置视频id
            request.setVideoIds(videoId);
            //client调用方法删除
            DeleteVideoResponse response = client.getAcsResponse(request);
            String requestId = response.getRequestId();
            if (requestId != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new YangoException(20001, "删除视频资源失败!");
        }
    }

    //根据id批量删除视频
    @Override
    public boolean removeVideoByIds(List videoIdList) {
        try {
            //创建客户端client对象
            DefaultAcsClient client = AliyunVodSDKUtil.initVodClient(ReadPropertiesUtil.ACCESS_KEY_ID,
                    ReadPropertiesUtil.ACCESS_KEY_SECRET);
            //创建请求request对象
            //一次只能批量删除20个
            //将idlist集合转换成array数组
            String videoIds = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request对象中设置视频id
            request.setVideoIds(videoIds);
            //client调用方法删除获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);
            String requestId = response.getRequestId();
            if (requestId != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new YangoException(20001, "删除视频资源失败!");
        }
    }
}
